package com.joinlabs.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.joinlabs.entity.Order;
import com.joinlabs.entity.OrderSystemer;
import com.joinlabs.entity.Orderitem;
import com.joinlabs.entity.User;
import com.joinlabs.entity.Wares;
import com.joinlabs.mapper.OrderMapper;
import com.joinlabs.mapper.OrderitemMapper;
import com.joinlabs.mapper.UserMapper;
import com.joinlabs.mapper.WaresMapper;
import com.joinlabs.service.IUserSystemer;

@Service("UserSystemerService")
public class UserSystemerImpl implements IUserSystemer {
	@Resource
	private WaresMapper waresMapper;
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private OrderMapper orderMapper;
	
	@Resource
	private OrderitemMapper orderitemMapper;
//删除用户显示界面	
	@Override
	public List<User> findUsersinfoAll(int a) {
		// TODO Auto-generated method stub
		if(a == 1) {
			int userPart = 1;
		List<User>users =  userMapper.findUsersinfoAll(userPart);
		return users;
		}
		if(a == 0) {
			int userPart = 0;
		List<User>users =  userMapper.findUsersinfoAll(userPart);
		return users;
		}
		return null;
	}

//批量删除
	@Override
	public void delUsersbyUserid(String[] strs) {
		// TODO Auto-generated method stub
		for (String userId : strs) {
			userMapper.deleteByPrimaryKey(userId);
			//waresMapper.deleteByPrimaryKey(pid);
		}
	}

//删除用户搜索
	@Override
	public List<User> selectInfobywaresName(String userName) {
		// TODO Auto-generated method stub
		String likeName = "%"+userName+"%";
		List<User>users =userMapper.selectInfobywaresName(likeName);
		return users;
	}

	@Override
	public void insertInfobyUser(User user) {
		// TODO Auto-generated method stub
		user.setUserPart(0);
		userMapper.insertSelective(user);
		
	}
//查询所有待发货的订单子项
	@Override
	public List<OrderSystemer> findOrderbyorderState() {
		// TODO Auto-generated method stub
		List<OrderSystemer> orderSystemers = new ArrayList<OrderSystemer>();
		//查询所有订单子项
		List<Orderitem>  orderitems = orderitemMapper.selectOrderiteminfoAll();
		//System.out.println(orderitems.size());
		for (Orderitem orderitem : orderitems) {
			//System.out.println(orderitem.getOrderId());
			String orderId = orderitem.getOrderId();
			//System.out.println("orderId:"+orderId);
			Order order = orderMapper.selectByPrimaryKey(orderId);
			//System.out.println(order);
			if(order.getOrderState() == 1) {
			OrderSystemer orderSystemer = new OrderSystemer();
			//orderSystemer.setUserId()
			Wares wares = waresMapper.selectByPrimaryKey(orderitem.getWaresId());
			//User user = userMapper
			orderSystemer.setOrderId(orderitem.getOrderId());
			orderSystemer.setWaresId(orderitem.getWaresId());
			orderSystemer.setImgpath(orderitem.getImgpath());
			orderSystemer.setWaresName(wares.getWaresName());
			orderSystemer.setOrderItemid(orderitem.getOrderItemid());
			orderSystemer.setOrderState(order.getOrderState());
			orderSystemer.setUserId(order.getUserId());
			//System.out.println(orderSystemer);
			orderSystemers.add(orderSystemer);
			//System.out.println(orderSystemers.size());
			}
			
		}
		return orderSystemers;
	}

//修改订单状态为已发货
	@Override
	public void updateOrderStatebyOrderId(String[] id) {
		// TODO Auto-generated method stub
		for (String orderId : id) {
			orderMapper.updateOrderStatebyOrderId(orderId);
		}
		
	}

	@Override
	public int selectOrderiteminfoAll() {

		List<Orderitem> orderlist = orderitemMapper.selectOrderiteminfoAll();

		int zmoney = 0;
		
		for(int i = 0;i < orderlist.size();i++) {
			
			BigDecimal count = orderlist.get(i).getWaresCount();
			int count1 = count.intValue();
			
			BigDecimal money = new BigDecimal (orderlist.get(i).getOrderPrice());
			int money1 = money.intValue();
			
			int zj = money1 * count1;
			
			zmoney = zmoney+zj;
		}
		
		
		return zmoney;
	}

	@Override
	public int findUsersinfoAlluser() {

		int count = userMapper.findUsersinfoAlluser();
		
		return count;
	}

	
}
